package com.koron.css2.baseConfig.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.BaseAccessoryMetadataBean;
import com.koron.css2.baseConfig.bean.BaseReceiptAccessoryBean;
import com.koron.css2.baseConfig.bean.ContractTemplateBean;
import com.koron.css2.baseConfig.mapper.BaseAccessoryMetadataMapper;
import com.koron.css2.baseConfig.mapper.BaseReceiptAccessoryMapper;
import com.koron.css2.baseConfig.mapper.ContractTemplateMapper;
import com.koron.css2.baseConfig.queryBean.BaseReceiptAccessoryQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
/**
 * 合同模板列表
 * @author 10950
 *
 */
public class ContractTemplateList implements ServerInterface{

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		 @SuppressWarnings("rawtypes")
	        MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
	        try {
	        	String type="";
	        	Map bean =  JsonUtils.objectToPojo(req.getData(),Map.class);
	        	ContractTemplateMapper mapper = factory.getMapper(ContractTemplateMapper.class);
	        	PageHelper.startPage(Integer.parseInt(bean.get("page").toString()) , Integer.parseInt(bean.get("pageCount").toString()));
	        	if(bean.get("type") != null) {
	        		type = bean.get("type").toString();
	        	}
	        	List<ContractTemplateBean> cdList = mapper.selectList(type);
				Map<String, String> signType = Tools.mapDicByCode(factory, "signType");
				Map<String, String> type_contract = Tools.mapDicByCode(factory, "type_contract");


				cdList.stream().forEach(s->{
					if(s.getSignType()!=null){
						s.setSignType(signType.get(s.getSignType()).trim());
					}
					if(s.getTypeContract()!=null){
						s.setTypeContract(type_contract.get(s.getTypeContract()).trim());
					}
				});

	        	BaseReceiptAccessoryMapper accessoryMapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
	        	BaseAccessoryMetadataMapper accessoryMetadataMapper = factory.getMapper(BaseAccessoryMetadataMapper.class);
	        	
	        	if(cdList!=null && cdList.size()>0) {
	        		for(ContractTemplateBean templateBean : cdList) {
	        			BaseReceiptAccessoryQueryBean query = new BaseReceiptAccessoryQueryBean();
		        		query.setReceiptId(templateBean.getId());
		        		List<BaseReceiptAccessoryBean> receiptAccessorys = accessoryMapper.selectList(query);
		        		if(receiptAccessorys !=null && receiptAccessorys.size()>0) {
		        			BaseAccessoryMetadataBean accessoryMetadata = accessoryMetadataMapper.selectById(receiptAccessorys.get(0).getMetadataId());
		        			templateBean.setFjName(ApplicationConfig.getAccessoryUploadUrl() + "fileDownload?path=" + accessoryMetadata.getAccessoryPath());
		        		}
	        		}
	        		
	        	}
	            info.setCode(Constant.MESSAGE_INT_SUCCESS);
	            info.setDescription("success");
	            info.setData(new PageInfo<>(cdList));
	        } catch (Exception e) {
	            info.setCode(Constant.MESSAGE_INT_FAIL);
	            info.setDescription("操作失败");
	            logger.error("操作失败", e);
	        }
	        return info;
	}
	
}
